package cn.tedu.car.backend.station.service;

import cn.tedu.car.backend.base.exception.ServiceException;
import cn.tedu.car.backend.base.response.StatusCode;
import cn.tedu.car.backend.station.mapper.StationMapper;
import cn.tedu.car.backend.station.pojo.dto.StationQuery;
import cn.tedu.car.backend.station.pojo.dto.StationSaveParam;
import cn.tedu.car.backend.station.pojo.dto.StationUpdateParam;
import cn.tedu.car.backend.station.pojo.entity.Station;
import cn.tedu.car.backend.station.pojo.vo.StationVO;
import cn.tedu.car.backend.vehicleStation.mapper.VehicleMapper1;
import cn.tedu.car.backend.vehicleStation.pojo.dto.VehicleBindDTO;
import cn.tedu.car.backend.vehicleStation.service.VehicleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class StationServiceImpl implements StationService{
    @Autowired
    private StationMapper stationMapper;
    @Autowired
    private VehicleService vehicleService;
    @Autowired
    private VehicleMapper1 vehicleMapper1;
    @Override
    public void addStation(StationSaveParam stationSaveParam) {
        //新增站点方法
        Station station=new Station();
        stationSaveParam.setUpdateTime(new Date());
        //将stationSaveParam的属性值复制给station
        BeanUtils.copyProperties(stationSaveParam,station);
        //按照绑定车辆id数列的大小设置绑定车辆数量
        station.setBindVehicle(stationSaveParam.getBindVehicleList().size());
        log.debug("新增站点输入参数station：{}",station);
        //首先创建新站点，获取对应的站点id，用来设置车辆表中的数据
        int addStation = stationMapper.addStation(station);
        if (addStation == 0){
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }
        long stationId=station.getId();
        //新建一个车辆绑定的对象，用于绑定站点和车辆
        VehicleBindDTO vehicleBindDTO=new VehicleBindDTO();
        vehicleBindDTO.setOutletsId(stationId);
        //循环取出绑定车辆列表的id，将所有车辆的站点id进行设置
        for (Long id: stationSaveParam.getBindVehicleList()){
            vehicleBindDTO.setId(id);
            vehicleMapper1.bindVehicle(vehicleBindDTO);
        }
        //设置完成后将目前状态为可用的车辆的数量读取回来，设置站点的可用车辆数量
        Integer usableVehicleCount = vehicleMapper1.usableVehicleCount(stationId);
        station.setUsableVehicle(usableVehicleCount);
        log.debug("更新站点输入参数station：{}",station);
        //更新站点信息，将可用车辆数量进行更新
        int updateStationInfo = stationMapper.updateStationInfo(station);
        if (updateStationInfo == 0){
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }
    }



    @Override
    public List<StationVO> loadStation(StationQuery stationQuery) {
        log.debug("查询站点信息:{}",stationQuery);
        //查询站点信息，若查询条件为空将所有站点信息返回，若存在查询条件将对应站点信息返回
        List<StationVO> stationVOList=stationMapper.loadStation(stationQuery);
        log.debug("返回站点信息:{}",stationVOList);
        return stationVOList;
    }

    @Override
    public void updateStation(StationUpdateParam stationUpdateParam) {
        log.debug("更新站点信息参数:{}",stationUpdateParam);
        //更新站点信息
        Station station=new Station();
        stationUpdateParam.setUpdateTime(new Date());
        //将站点更新的信息传递给封装函数，进行车辆信息的更新
        updateVehicleInfo(stationUpdateParam);
        //将更新完后的更新信息赋值给station对象
        BeanUtils.copyProperties(stationUpdateParam,station);
        //更新station对象
        Integer updateStationInfo = stationMapper.updateStationInfo(station);
        if (updateStationInfo == 0){
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }
    }

    @Override
    public void deleteStation(Long id) {
        log.debug("删除站点参数:{}",id);
        //根据站点id删除站点信息
        Integer deleteStation = stationMapper.deleteStation(id);
        //若站点成功删除，则根据站点id解绑对应的车辆，若未成功则不进行操作
        if (deleteStation != 0){
            Integer removeBindVehicle = vehicleMapper1.removeBindVehicle(id);
            if (removeBindVehicle == null){
                throw new ServiceException(StatusCode.OPERATION_FAILED);
            }
        }
    }

    private void updateVehicleInfo(StationUpdateParam stationUpdateParam){
        //更新站点信息的封装方法
        log.debug("更新车辆信息参数:{}",stationUpdateParam);
        //新建一个车辆的对象，用于更新车辆绑定信息
        VehicleBindDTO vehicleBindDTO=new VehicleBindDTO();
        vehicleBindDTO.setOutletsId(stationUpdateParam.getId());
        //循环站点绑定车辆id列表，将新增的绑定车辆进行绑定操作
        for (Long id : stationUpdateParam.getBindVehicleList()){
            vehicleBindDTO.setId(id);
            vehicleMapper1.bindVehicle(vehicleBindDTO);
        }
        //设置完成后将目前状态为可用的车辆的数量读取回来，设置站点的可用车辆数量
        Integer usableVehicleCount = vehicleMapper1.usableVehicleCount(stationUpdateParam.getId());
        stationUpdateParam.setBindVehicle(stationUpdateParam.getBindVehicleList().size());
        stationUpdateParam.setUsableVehicle(usableVehicleCount);
    }
}
